#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>

void test1(){
    //输出重定向
    close(1);
    int fd = open("redirection.txt", O_WRONLY | O_CREAT, 0666);
    if(fd < 0){
        perror("open");
        return;
    }
    printf("hello cpp\n");
    printf("hello cpp\n");
    printf("hello cpp\n");
    fflush(stdout);

    close(fd);
}


void test2(){
    close(0);
    int fd = open("redirection.txt", O_RDONLY | O_CREAT, 0666);
    if(fd < 0){
        perror("open");
        return;
    }
    char str[64];
    while(scanf("%s", str) != EOF){
        printf("%s\n", str);
    }
    close(fd);
}

void test3(){
    close(0);
    close(1);
    int fd1 = open("redirection.txt", O_RDONLY | O_CREAT, 0666);
    int fd2 = open("re2.txt", O_WRONLY | O_CREAT | O_APPEND, 0666);
    if(fd1 < 0){
        perror("open");
        return;
    }
    char str[64];
    while(scanf("%s", str) != EOF){
        printf("%s\n", str);
    }
    fflush(stdout);
    close(fd1);
    close(fd2);
}

int main(){
    test3();
    return 0;
}